package a.b.reggie.controller;

import a.b.reggie.common.R;
import a.b.reggie.dto.SetmealDto;
import a.b.reggie.pojo.Category;
import a.b.reggie.pojo.Setmeal;
import a.b.reggie.pojo.SetmealDish;
import a.b.reggie.service.ICategoryService;
import a.b.reggie.service.ISetmealDishService;
import a.b.reggie.service.ISetmealService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 套餐管理
 */
@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    private ISetmealService setmealService;

    @Autowired
    private ISetmealDishService setmealDishService;

    @Autowired
    private ICategoryService categoryService;

    /**
     * 获取分页信息
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public R<Page<SetmealDto>> page(int page,int pageSize,String name){
        //构造分页构造器
        Page<Setmeal> pageInfo = new Page<>(page,pageSize);
        Page<SetmealDto> dtoPage = new Page<>();

        //设置分页条件
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(name!=null,Setmeal::getName,name);
        queryWrapper.orderByDesc(Setmeal::getPrice);

        //获取分页信息
        setmealService.page(pageInfo,queryWrapper);

        //复制分页基本信息
        BeanUtils.copyProperties(pageInfo,dtoPage,"records");

        //获取分页套餐信息
        List<Setmeal> records = pageInfo.getRecords();
        List<SetmealDto> dtoList = new ArrayList<>();

        //复制套餐信息
        for (Setmeal setmeal:records){
            //复制套餐基本信息
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(setmeal,setmealDto);

            //获取分类id
            Long categoryId = setmeal.getCategoryId();

            //根据分类id获取分类name
            LambdaQueryWrapper<Category> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.eq(Category::getId,categoryId);
            Category category = categoryService.getOne(queryWrapper1);
            String categoryName = category.getName();

            //设置套餐的分类名称
            setmealDto.setCategoryName(categoryName);
            dtoList.add(setmealDto);
        }
        //设置分页套餐信息
        dtoPage.setRecords(dtoList);

        return R.success(dtoPage);
    }

    /**
     * 添加套餐，以及与菜品的映射
     * @param setmealDto
     * @return
     */
    @PostMapping()
    public R<String> save(@RequestBody SetmealDto setmealDto){
        setmealService.saveWithDish(setmealDto);
        return R.success("添加成功");
    }

    /**
     * 删除套餐，以及与菜品的映射
     * @param ids
     * @return
     */
    @DeleteMapping
    @CacheEvict(value = "Setmeal",allEntries = true)
    public R<String> delete(@RequestParam List<Long > ids){
        //将数组转化为集合
        //List<Long> list = Arrays.asList(ids);
        setmealService.removeWithDish(ids);
        return R.success("删除成功");
    }

    /**
     * 根据id查询套餐
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<SetmealDto> getById(@PathVariable Long id){
        //查询套餐基本信息
        Setmeal setmeal = setmealService.getById(id);
        //复制套餐基本信息
        SetmealDto setmealDto = new SetmealDto();
        BeanUtils.copyProperties(setmeal,setmealDto);
        //查询菜品信息
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(SetmealDish::getSetmealId,id);
        List<SetmealDish> list = setmealDishService.list(queryWrapper);
        //设置菜品信息
        setmealDto.setSetmealDishes(list);

        return R.success(setmealDto);
    }

    /**
     * 修改套餐，及其菜品
     * @param setmealDto
     * @return
     */
    @PutMapping
    @CacheEvict(value = "Setmeal",allEntries = true)
    public R<String> update(@RequestBody SetmealDto setmealDto){
        setmealService.updateWithDish(setmealDto);
        return R.success("修改成功");
    }


    /**
     * 更改售卖状态
     * @param num
     * @param ids
     * @return
     */
    @PostMapping("/status/{num}")
    @CacheEvict(value = "Setmeal",allEntries = true)
    public R<String> status(@PathVariable int num,Long[] ids){
        setmealService.status(num,ids);
        return R.success("更改成功");
    }

    /**
     * 根据分类查询套餐集合
     * @param categoryId
     * @param status
     * @return
     */
    @GetMapping("/list")
    @Cacheable(value = "Setmeal" ,key = "#categoryId + '_' + #status")
    public R<List<Setmeal>> getListByCategoryId(Long categoryId,int status){
       List<Setmeal> list = setmealService.getListByCategoryId(categoryId,status);
        return R.success(list);
    }

    /**
     * 根据套餐id或食品id查询
     * @param id
     * @return
     */
    @GetMapping("/dish/{id}")
    public R get(@PathVariable Long id){
        return R.success(setmealService.getDish(id));
    }



}
